<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:jsf="http://xmlns.jcp.org/jsf"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <h:head>
    </h:head>

    <h:body>
        <f:metadata>
            <f:viewParam name="dataverseId" value="#{dataverseServiceBean.findRootDataverse().id}"/>
            <f:viewAction action="#{dataverseSession.updateLocaleInViewRoot}"/>
            <f:viewAction action="#{DashboardUsersPage.init}"/>
            <f:viewAction action="#{dataverseHeaderFragment.initBreadcrumbs(dataverseServiceBean.findRootDataverse())}"/>
            <f:viewAction action="#{dataverseHeaderFragment.addBreadcrumb('/dashboard.xhtml?dataverseId='.concat(dataverse.id), bundle['dashboard.title'])}"/> 
            <f:viewAction action="#{dataverseHeaderFragment.addBreadcrumb(bundle['dashboard.card.users.manage'])}"/>
        </f:metadata>

        <ui:composition template="/dataverse_template.xhtml">
            <ui:param name="pageTitle" value="#{bundle['dashboard.card.users.header']} - #{dataverseServiceBean.findRootDataverse().name} #{bundle.dataverse}"/>
            <ui:param name="showDataverseHeader" value="false"/>
            <ui:define name="body">
                <h:form id="dashboardUsersForm">
                    <p:focus for="searchFiles"/>
                    <div jsf:id="userssHeaderBlock" class="row">
                        <div class="col-xs-6">
                            <div class="input-group">
                                <p:inputText type="hidden" id="selectedPage" value="#{DashboardUsersPage.selectedPage}" />
                                <p:inputText id="searchFiles" styleClass="form-control" value="#{DashboardUsersPage.searchTerm}" widgetVar="inputSearchTerm"
                                             onkeypress="if (event.keyCode == 13) { submitsearch(); return false; }"/>
                                <p:watermark for="searchFiles" value="#{bundle['dashboard.list_users.searchTerm.watermark']}"/>
                                <p:remoteCommand name="submitsearch" action="#{DashboardUsersPage.runUserSearch()}" process="@this @widgetVar(inputSearchTerm)" update="@form" partialSubmit="true"/>
                                <span class="input-group-btn">
                                    <p:commandLink styleClass="btn btn-default" action="#{DashboardUsersPage.runUserSearch()}" process="@this @widgetVar(inputSearchTerm)" update="@form" partialSubmit="true">
                                        <span class="glyphicon glyphicon-search"></span> #{bundle['dataverse.search.btn.find']}
                                    </p:commandLink>
                                </span>
                            </div>
                        </div>
                    </div>

                    <ui:include src="dashboard-users-pager.xhtml">
                        <ui:param name="listPager" value="#{DashboardUsersPage.pager}" />
                    </ui:include>

                    <p:dataTable id="userTable" value="#{DashboardUsersPage.userList}" var="user" widgetVar="userTable">
                        <p:column style="width:60px" class="text-center" headerText="#{bundle['dashboard.list_users.tbl_header.userId']}">
                            <h:outputText value="#{user.id}" />
                        </p:column>
                        <p:column headerText="#{bundle['dashboard.list_users.tbl_header.userIdentifier']}">
                            <span class="glyphicon glyphicon-user text-danger" jsf:rendered="#{user.superuser}" style="margin-right:6px;"></span>
                            <h:outputText value="#{user.userIdentifier}" class="#{user.superuser ? 'text-danger' : ''}" />
                        </p:column>
                        <p:column headerText="#{bundle['dashboard.list_users.tbl_header.name']}">
                            <h:outputText value="#{user.firstName} #{user.lastName}" />
                        </p:column>
                        <p:column width="20%" headerText="#{bundle['dashboard.list_users.tbl_header.email']}">
                            <h:outputText value="#{user.email}" />
                        </p:column>
                        <p:column headerText="#{bundle['dashboard.list_users.tbl_header.affiliation']}">
                            <h:outputText value="#{user.affiliation}" />
                        </p:column>
                        <p:column width="12%" headerText="#{bundle['dashboard.list_users.tbl_header.authProviderFactoryAlias']}">
                            <h:outputText value="#{DashboardUsersPage.getAuthProviderFriendlyName(user.authProviderId)}" />
                        </p:column>
                        <p:column headerText="#{bundle['dashboard.list_users.tbl_header.roles']}">
                            <h:outputText value="#{user.roles}" />
                            <p:commandButton id="removeRolesButton" 
                                            rendered="#{!empty user.roles}" 
                                            value="#{bundle['dashboard.list_users.tbl_header.roles.removeAll']}" 
                                            styleClass="btn btn-default">
                                <p:ajax event="click" 
                                        oncomplete="PF('removeRolesConfirm').show();"
                                        process="@this" 
                                        listener="#{DashboardUsersPage.setUserToToggleSuperuserStatus(user)}"
                                        update=":dashboardUsersForm:removeRolesConfirm"/>
                            </p:commandButton>
                        </p:column>
                        <p:column width="9%" class="text-center" headerText="#{bundle['dashboard.list_users.tbl_header.isSuperuser']}">
                            <!-- A simple implementation of the superuser status toggle - via a boolean checkbox with an immediate ajax update. -->
                            <!-- Uses our standard approach, of showing a confirmation popup ("are you sure you want to toggle this? ...") first,  -->
                            <!-- before saving the toggled value. -->
                            <!-- The current user, the admin user viewing the dashboard, should not be allowed to toggle the superuser status on him- or herself: -->
                            <p:selectBooleanCheckbox id="superUserCheckbox" value="#{user.superuser}" valueChangeListener="#{DashboardUsersPage.setUserToToggleSuperuserStatus(user)}"  disabled="#{user.id == dataverseSession.user.id}">
                                <p:ajax event="change" process="@this" oncomplete="PF('toggleSuperuserConfirmation').show();"  update=":dashboardUsersForm:toggleSuperuserConfirmation"/>
                            </p:selectBooleanCheckbox>
                        </p:column>
                    </p:dataTable>
                    <p:dialog styleClass="smallPopUp" id="toggleSuperuserConfirmation" header="#{bundle['dashboard.list_users.toggleSuperuser']}" widgetVar="toggleSuperuserConfirmation" modal="true" closable="false">
                        <p:focus context="toggleSuperuserConfirmation"/>
                        <p class="text-warning"><span class="glyphicon glyphicon-warning-sign"/>
                            <h:outputFormat value=" #{bundle['dashboard.list_users.toggleSuperuser.confirmationText.add']} " rendered="#{DashboardUsersPage.selectedUserDetached.superuser}">
                                <f:param value="#{DashboardUsersPage.selectedUserDetached.userIdentifier}"/>
                            </h:outputFormat>
                            <h:outputFormat value=" #{bundle['dashboard.list_users.toggleSuperuser.confirmationText.remove']} " rendered="#{!DashboardUsersPage.selectedUserDetached.superuser}">
                                <f:param value="#{DashboardUsersPage.selectedUserDetached.userIdentifier}"/>
                            </h:outputFormat>
                        </p>
                        <div class="button-block">
                            <p:commandButton styleClass="btn btn-default" value="#{bundle.yes}" action="#{DashboardUsersPage.saveSuperuserStatus()}" update="userTable,:messagePanel" onclick="PF('toggleSuperuserConfirmation').hide();"/>
                            <p:commandButton styleClass="btn btn-link" value="#{bundle.no}" action="#{DashboardUsersPage.cancelSuperuserStatusChange()}" update=":dashboardUsersForm:userTable,:messagePanel" onclick="PF('toggleSuperuserConfirmation').hide();"/>
                        </div>
                    </p:dialog>
                    <p:dialog id="removeRolesConfirm" header="#{bundle['dashboard.list_users.tbl_header.roles.removeAll.header']}" widgetVar="removeRolesConfirm" modal="true" closable="false">
                        <p class="text-warning"><span class="glyphicon glyphicon-warning-sign"/> #{DashboardUsersPage.confirmRemoveRolesMessage}</p>
                        <div class="button-block">
                            <p:commandButton styleClass="btn btn-default" value="#{bundle.yes}" update="userTable,:messagePanel" oncomplete="PF('removeRolesConfirm').hide();" action="#{DashboardUsersPage.removeUserRoles()}"/>
                            <button class="btn btn-link" onclick="PF('removeRolesConfirm').hide()" type="button">
                                #{bundle.no}
                            </button>
                        </div>
                    </p:dialog>
                </h:form>
            </ui:define>
        </ui:composition>

    </h:body>
</html>
